<?php

use models\Business\AlibabaSku;
use models\DAO\BookingModel;
use Yaf\Session;
use Yaf\Registry;
use models\Business\AlibabaProduct;
use models\Business\Coupon;
use models\Business\Alibaba;
use ylAlibaba\AliOpen;
use models\Exception\Business\AlibabaproductException;


class AlibabaproductController extends ApiBaseController
{
    /**
     * @SWG\Get(
     *     path="/admin/alibabaproduct/list",
     *     tags={"1688商品"},
     *     summary="获取1688商品列表数据接口",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="page",
     *          description="页码",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="page_size",
     *          description="每页显示条数",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="redemption",
     *          description="是否为积分兑换商品 可选值: 0为否|1为是",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sort",
     *          description="排序方式 可选值: desc|asc",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="order",
     *          description="排序字段 可选值: id",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/alibabaproductListData")
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function listAction() {
        $parameters = Registry::get('parameters');

        $products = AlibabaProduct::getProductList($parameters);

        return $this->_responseJson($products);
    }

    /**
     * @SWG\Get(
     *     path="/admin/alibabaproduct/{product_id}/info",
     *     tags={"1688商品"},
     *     summary="获取1688商品详情接口",
     *     description="",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="product_id",
     *          description="/admin/Alibabaproduct/list接口返回的id字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/alibabaproductSingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="编辑失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function infoAction() {
        $parameters = Registry::get('parameters');

        $product = AlibabaProduct::getProduct($parameters);

        return $this->_responseJson(['data' => $product['data'][0]]);
    }

    /**
     * @SWG\Put(
     *     path="/admin/alibabaproduct/{id}/edit",
     *     tags={"1688商品"},
     *     summary="编辑1688商品接口",
     *     description="",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="product_id",
     *          description="/admin/Alibabaproduct/list接口返回的id字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="top_parent_id",
     *          description="顶级分类",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sub_parent_id",
     *          description="子分类",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="leaf_parent_id",
     *          description="叶级分类",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="title",
     *          description="商品标题",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="img_url",
     *          description="商品首图",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="images",
     *          description="主图列表(使用相对路径,需要增加域名:https://cbu01.alicdn.com/)",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="main_video",
     *          description="主图视频播放地址",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="stats",
     *          description="状态",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="redemption",
     *          description="允许积分兑换(0为否,1为是,默认0)",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="integral",
     *          description="兑换所需积分",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="platform_price",
     *          description="平台售价",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/alibabaproductSingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="编辑失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function editAction() {
        $parameters = Registry::get('parameters');
        $parameters['product_id'] = $parameters['id'];

        $product = AlibabaProduct::editProduct($parameters);

        if ($product['stats'] == 1) {
            // 调用接口 关注1688商品
            try {
                $ali = new AliOpen();
                $ali->params = ['productId' => $parameters['id']];
                $ali->setAppKey(Alibaba::APP_KEY);
                $ali->setAppSecret(Alibaba::APP_SECRET);

                // 测试时使用本地接口 正式接口调用时 注释掉下面两行
//                $ali->product->setMode('development');
//                $ali->product->res_url = "http://www.ylmall.com/common/alibabatest/follow";

                $ali->product->productFollow();
            } catch (Exception $e) {
                AlibabaProduct::editProduct(['product_id' => $parameters['id'], 'stats' => 0]);
                // 删除商品缓存
                Registry::get('redis_string')->deleteString("alibaba_product:" . $parameters['id']);
                throw new AlibabaproductException($e->getMessage(), 400);
            }

            $coupon = new Coupon($parameters['id'], $product['amount_on_sale'], '', '');
            $coupon->resetStore();

            $product_info = AlibabaSku::getSkuByModules(['data' => [$product]]);
            $sku_id_store_arr = array_column($product_info['data'][0]['sku'], 'amount_on_sale', 'sku_id');

            if (!empty($sku_id_store_arr)) {
                foreach ($sku_id_store_arr as $sku_id => $sku_store) {
                    // 重置缓存
                    $coupon = new Coupon($parameters['id'], $sku_store, '', $sku_id);
                    $coupon->resetStore();
                }
            }
        }

        return $this->_responseJson(['data' => $product]);
    }
}
